import * as THREE from '../three.js/build/three.module.js';
import { OrbitControls } from '../three.js/examples/jsm/controls/OrbitControls.js';

class BasicThreeScene {
    constructor() {
        this.scene = null;
        this.camera = null;
        this.renderer = null;
        this.controls = null;
        this.objects = [];
        
        this.init();
        this.createObjects();
        this.setupEventListeners();
        this.animate();
    }
    
    init() {
        const canvas = document.getElementById('canvas');
        
        // 创建场景 - 参考 three-labels-demo 的深蓝色背景
        this.scene = new THREE.Scene();
        this.scene.background = new THREE.Color(0x1a1a2e);
        
        // 创建相机
        this.camera = new THREE.PerspectiveCamera(
            75,
            window.innerWidth / window.innerHeight,
            0.1,
            1000
        );
        this.camera.position.set(10, 10, 10);
        
        // 创建渲染器
        this.renderer = new THREE.WebGLRenderer({ 
            canvas: canvas,
            antialias: true,
            alpha: true
        });
        this.renderer.setSize(window.innerWidth, window.innerHeight);
        this.renderer.shadowMap.enabled = true;
        this.renderer.shadowMap.type = THREE.PCFSoftShadowMap;
        
        // 创建控制器
        this.controls = new OrbitControls(this.camera, this.renderer.domElement);
        this.controls.enableDamping = true;
        this.controls.dampingFactor = 0.05;
        
        // 添加光源
        this.addLights();
    }
    
    addLights() {
        // 环境光
        const ambientLight = new THREE.AmbientLight(0x404040, 0.4);
        this.scene.add(ambientLight);
        
        // 方向光
        const directionalLight = new THREE.DirectionalLight(0xffffff, 1);
        directionalLight.position.set(10, 10, 5);
        directionalLight.castShadow = true;
        directionalLight.shadow.mapSize.width = 2048;
        directionalLight.shadow.mapSize.height = 2048;
        this.scene.add(directionalLight);
        
        // 点光源
        const pointLight = new THREE.PointLight(0xff6b6b, 0.5, 50);
        pointLight.position.set(-10, 10, -10);
        this.scene.add(pointLight);
    }
    
    createObjects() {
        // 参考 three-labels-demo 的几何体布局和材质
        const objectsData = [
            { 
                geometry: new THREE.BoxGeometry(2, 2, 2),
                material: new THREE.MeshStandardMaterial({ color: 0x667eea }),
                position: [-8, 0, 0]
            },
            {
                geometry: new THREE.SphereGeometry(1.5, 32, 32),
                material: new THREE.MeshStandardMaterial({ color: 0xff6b6b }),
                position: [-4, 0, 0]
            },
            {
                geometry: new THREE.ConeGeometry(1.5, 3, 8),
                material: new THREE.MeshStandardMaterial({ color: 0x00ff88 }),
                position: [0, 0, 0]
            },
            {
                geometry: new THREE.CylinderGeometry(1, 1, 2, 16),
                material: new THREE.MeshStandardMaterial({ color: 0xffa500 }),
                position: [4, 0, 0]
            },
            {
                geometry: new THREE.TorusGeometry(1, 0.4, 8, 16),
                material: new THREE.MeshStandardMaterial({ color: 0x9b59b6 }),
                position: [8, 0, 0]
            }
        ];
        
        // 创建3D对象
        objectsData.forEach((data) => {
            const mesh = new THREE.Mesh(data.geometry, data.material);
            mesh.position.set(...data.position);
            mesh.castShadow = true;
            mesh.receiveShadow = true;
            this.scene.add(mesh);
            this.objects.push(mesh);
        });
    }
    
    setupEventListeners() {
        // 窗口大小调整
        window.addEventListener('resize', () => this.onWindowResize());
    }
    
    onWindowResize() {
        this.camera.aspect = window.innerWidth / window.innerHeight;
        this.camera.updateProjectionMatrix();
        this.renderer.setSize(window.innerWidth, window.innerHeight);
    }
    
    animate() {
        requestAnimationFrame(() => this.animate());
        
        // 旋转所有对象
        this.objects.forEach((obj, index) => {
            obj.rotation.x += 0.01;
            obj.rotation.y += 0.01 * (index + 1) * 0.5;
        });
        
        // 更新控制器
        this.controls.update();
        
        // 渲染场景
        this.renderer.render(this.scene, this.camera);
    }
}

// 初始化场景
let scene;
window.addEventListener('DOMContentLoaded', () => {
    scene = new BasicThreeScene();
});

export { BasicThreeScene };